﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Cognex.VisionPro.CalibFix;
using Cognex.VisionPro.ImageFile;
using Cognex.VisionPro;
using Cognex.VisionPro.ToolBlock;

namespace Delta
{
    public partial class CalibNPointToNPointToolForm : Form
    {
        //定义字段
        public CogCalibNPointToNPointTool CalibNPointToNPointTool = new CogCalibNPointToNPointTool();
        public CogImageFileTool cogImageFile = new CogImageFileTool();
        public CogToolBlock cogToolBlock = new CogToolBlock();
        public CogRectangleAffine aROI = new CogRectangleAffine();
        public CogImage8Grey image;
        public CalibNPointToNPointToolForm()
        {
            InitializeComponent();
        }
        //训练按钮时间触发
        private void bTrainImage_Click(object sender, EventArgs e)
        {
            try
            {
                //模板匹配图像输入
                CalibNPointToNPointTool.InputImage = cogRecordDisplay1.Image;
                CalibNPointToNPointTool.CalibrationImage = CalibNPointToNPointTool.InputImage;              
               
                richTextBox1.Text += "抓取训练图像成功！\r\n";
                //cogPM.Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }

        private void bOpenImage_Click(object sender, EventArgs e)
        {
            try
            {
                cogRecordDisplay1.InteractiveGraphics.Clear();
                cogRecordDisplay1.StaticGraphics.Clear();
                string path;
                OpenFileDialog ofd = new OpenFileDialog(); // 实例一个打开文件的对象
                ofd.Filter = "bmpFile(*.bmp;*.jpg)|*.bmp;*.jpg"; // 实际起过滤作用的为|后面的部分
                if (ofd.ShowDialog() == DialogResult.OK) //弹出打开文件的弹窗，在点确定后执行
                {
                    path = ofd.FileName; //将文件路径传给变量 path
                    cogImageFile.Operator.Open(path, CogImageFileModeConstants.Read);//第一个为图片路径，第二个为打开的方式;
                    cogImageFile.Run();
                    image = (CogImage8Grey)cogImageFile.OutputImage;//若要用到CogImage8Grey，需要显式转换一次
                    cogRecordDisplay1.Image = image;//界面上显示打开的图像
                    cogRecordDisplay1.Fit(true);
                    richTextBox1.Text += "图像打开成功！\r\n";
                }
            }
            catch (Exception ex)
            {
                richTextBox1.Text += "图像打开失败！\r\n";
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void ShowNpointTONpoint()
        {
            double x, y;
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(0, out x, out y);
            dataGridView1.Rows[0].Cells[2].Value = x;
            dataGridView1.Rows[0].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(1, out x, out y);
            dataGridView1.Rows[1].Cells[2].Value = x;
            dataGridView1.Rows[1].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(2, out x, out y);
            dataGridView1.Rows[2].Cells[2].Value = x;
            dataGridView1.Rows[2].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(3, out x, out y);
            dataGridView1.Rows[3].Cells[2].Value = x;
            dataGridView1.Rows[3].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(4, out x, out y);
            dataGridView1.Rows[4].Cells[2].Value = x;
            dataGridView1.Rows[4].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(5, out x, out y);
            dataGridView1.Rows[5].Cells[2].Value = x;
            dataGridView1.Rows[5].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(6, out x, out y);
            dataGridView1.Rows[6].Cells[2].Value = x;
            dataGridView1.Rows[6].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(7, out x, out y);
            dataGridView1.Rows[7].Cells[2].Value = x;
            dataGridView1.Rows[7].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(8, out x, out y);
            dataGridView1.Rows[8].Cells[2].Value = x;
            dataGridView1.Rows[8].Cells[3].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(0, out x, out y);
            dataGridView1.Rows[0].Cells[0].Value = x;
            dataGridView1.Rows[0].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(1, out x, out y);
            dataGridView1.Rows[1].Cells[0].Value = x;
            dataGridView1.Rows[1].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(2, out x, out y);
            dataGridView1.Rows[2].Cells[0].Value = x;
            dataGridView1.Rows[2].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(3, out x, out y);
            dataGridView1.Rows[3].Cells[0].Value = x;
            dataGridView1.Rows[3].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(4, out x, out y);
            dataGridView1.Rows[4].Cells[0].Value = x;
            dataGridView1.Rows[4].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(5, out x, out y);
            dataGridView1.Rows[5].Cells[0].Value = x;
            dataGridView1.Rows[5].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(6, out x, out y);
            dataGridView1.Rows[6].Cells[0].Value = x;
            dataGridView1.Rows[6].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(7, out x, out y);
            dataGridView1.Rows[7].Cells[0].Value = x;
            dataGridView1.Rows[7].Cells[1].Value = y;

            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(8, out x, out y);
            dataGridView1.Rows[8].Cells[0].Value = x;
            dataGridView1.Rows[8].Cells[1].Value = y;


        }

            private void  SetNpointTONpoint()
        {
            double x, y;

            x =  Convert.ToDouble(dataGridView1.Rows[0].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[0].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(0, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(0, out x, out y);          
            

           
            x = Convert.ToDouble(dataGridView1.Rows[1].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[1].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(1, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(1, out x, out y);

           
            x = Convert.ToDouble(dataGridView1.Rows[2].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[2].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(2, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(2, out x, out y);

           
            x = Convert.ToDouble(dataGridView1.Rows[3].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[3].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(3, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(3, out x, out y);


          
            x = Convert.ToDouble(dataGridView1.Rows[4].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[4].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(4, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(4, out x, out y);

           
            x = Convert.ToDouble(dataGridView1.Rows[5].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[5].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(5, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(5, out x, out y);

         
            x = Convert.ToDouble(dataGridView1.Rows[6].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[6].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(6, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(6, out x, out y);

            x = Convert.ToDouble(dataGridView1.Rows[7].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[7].Cells[3].Value);

            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(7, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(7, out x, out y);

           
            x = Convert.ToDouble(dataGridView1.Rows[8].Cells[2].Value);
            y = Convert.ToDouble(dataGridView1.Rows[8].Cells[3].Value);
            CalibNPointToNPointTool.Calibration.SetRawCalibratedPoint(8, x, y);
            CalibNPointToNPointTool.Calibration.GetRawCalibratedPoint(8, out x, out y);
          
            x = Convert.ToDouble(dataGridView1.Rows[0].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[0].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(0, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(0, out x, out y);

           
            x = Convert.ToDouble(dataGridView1.Rows[1].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[1].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(1, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(1, out x, out y);

            x = Convert.ToDouble(dataGridView1.Rows[2].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[2].Cells[1].Value);

            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(2, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(2, out x, out y);

          
            x = Convert.ToDouble(dataGridView1.Rows[3].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[3].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(3, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(3, out x, out y);


           
            x = Convert.ToDouble(dataGridView1.Rows[4].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[4].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(4, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(4, out x, out y);

          
            x = Convert.ToDouble(dataGridView1.Rows[5].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[5].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(5, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(5, out x, out y);

      
            x = Convert.ToDouble(dataGridView1.Rows[6].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[6].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(6, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(6, out x, out y);

           
            x = Convert.ToDouble(dataGridView1.Rows[7].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[7].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(7, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(7, out x, out y);

          
            x = Convert.ToDouble(dataGridView1.Rows[8].Cells[0].Value);
            y = Convert.ToDouble(dataGridView1.Rows[8].Cells[1].Value);
            CalibNPointToNPointTool.Calibration.SetUncalibratedPoint(8, x, y);
            CalibNPointToNPointTool.Calibration.GetUncalibratedPoint(8, out x, out y);

        }


        private void bTrain_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.CalibNPointToNPointTool.Calibration != null && this.CalibNPointToNPointTool.CalibrationImage!= null )//首先判断工具是否为空
                {

                    SetNpointTONpoint();
                    CalibNPointToNPointTool.Calibration.Calibrate();                
                    Cognex.VisionPro.ICogRecord tmpRecord = CalibNPointToNPointTool.CreateCurrentRecord();//初始化Cognex记录工具，用以记录模板工具状态
                    //cogPM.Pattern.TrainImage = cogPM.InputImage;
                    //tmpRecord = tmpRecord.SubRecords[""];
                    cogRecordDisplay1.Record = tmpRecord;//显示训练原点
                    cogRecordDisplay1.Fit(true);
                    richTextBox1.Text += "标定成功！\r\n";
                }
                else
                {
                    richTextBox1.Text += "标定失败！\r\n";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string path = Application.StartupPath + @"\Vpp";
           
            CalibNPointToNPointTool = CogSerializer.LoadObjectFromFile(path + @"\Calibration.vpp") as CogCalibNPointToNPointTool;
            for (int i = 0; i < 9; i++)
            {
                dataGridView1.Rows.Add();//添加空白行            
            }
            ShowNpointTONpoint();
        }
        //高级设置显示完整的模板工具界面
        private void bAdvancedSet_Click(object sender, EventArgs e)
        {
            //string path = Application.StartupPath + @"\Vpp";
            //Form2 form2 = new Form2(this);           
            //form2.cogCalibNPointToNPointEditV21.Controls.RemoveAt(2);
            //CalibNPointToNPointTool = CogSerializer.LoadObjectFromFile(path + @"\Calibration.vpp") as CogCalibNPointToNPointTool;
            //form2.cogCalibNPointToNPointEditV21.Subject = CalibNPointToNPointTool;
            //form2.Show();
        }
        //模板匹配运行
        private void bRun_Click(object sender, EventArgs e)
        {
            try
            {
                CalibNPointToNPointTool.InputImage = image;
                CalibNPointToNPointTool.Run();
                lblRMS.Text = CalibNPointToNPointTool.Calibration.ComputedRMSError.ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
       

        private void bSave_Click(object sender, EventArgs e)
        {
            CogSerializer.SaveObjectToFile(CalibNPointToNPointTool, Application.StartupPath + @"\Vpp" + @"\Calibration.vpp");
            richTextBox1.Text += "工具保存成功！\r\n";
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //滚动信息显示最新一行
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.SelectionStart = richTextBox1.TextLength;
            richTextBox1.ScrollToCaret();
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string value = e.FormattedValue?.ToString();
            // 检查是否为空（如果允许为空则去掉此检查）
            if (string.IsNullOrWhiteSpace(value))
            {
                dataGridView1.Rows[e.RowIndex].ErrorText = "不能为空";
                e.Cancel = true;
                return;
            }
            // 验证是否为数字
            if (!double.TryParse(value, out _))
            {
                dataGridView1.Rows[e.RowIndex].ErrorText = "必须输入有效数字";
                e.Cancel = true;
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {           
                if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
                {
                    dataGridView1.BeginEdit(true);
                }
            
        }
    }
}

